package com.mm.controller;

import com.mm.domain.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class RibbonController {
    private static Logger logger = LoggerFactory.getLogger(RibbonController.class);
    private final String CALL_SERVICE = "http://MMSPRINGBOOT/test";

    @Autowired
    RestTemplate restTemplate;

    @GetMapping(value = "/ribbon-consumer")
    public String helloConsumer() {
        return restTemplate.getForEntity(CALL_SERVICE + "/getString", String.class).getBody();
    }

    //测试返回的响应类型,GET,POST,PUT,DELETE都一样
    @GetMapping(value = "/testResponseType")
    public void testResponseType() {
        logger.info("testResponseType_测试返回的响应类型");
        //返回字符串
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(CALL_SERVICE + "/get_String1", String.class);
        logger.info("get_String1:" + responseEntity.getBody());
        ResponseEntity<String> responseEntity2 = restTemplate.getForEntity(CALL_SERVICE + "/get_String2?name={1}", String.class, "mm");
        logger.info("get_String2:" + responseEntity2.getBody());
        //返回对象
        ResponseEntity<User> responseEntity3 = restTemplate.getForEntity(CALL_SERVICE + "/get_User", User.class);
        User user = responseEntity3.getBody();
        if(user != null){
            logger.info("get_User:" + user.getUsername());
        }
        //返回List,RestTemplate返回List泛型时需要用exchange,然后返回来类型封装在ParameterizedTypeReference里
        ResponseEntity<List<User>> responseEntity4 = restTemplate.exchange(CALL_SERVICE + "/get_List", HttpMethod.GET,null, new ParameterizedTypeReference<List<User>>(){});
        List<User> users= responseEntity4.getBody();
        for(User u : users){
            logger.info("get_List:" + u.getUsername());
        }
    }

    //测试GET的使用:getForEntity和getForObject
    @GetMapping(value = "/testGet")
    public void testGet() {
        logger.info("testGet_测试GET的使用");
        //getForEntity(String url, Class responseType, Object… urlVariables)
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(CALL_SERVICE + "/get_String2?name={1}", String.class, "mm");
        logger.info("getForEntity1:" + responseEntity.getBody());
        //getForEntity(String url, Class responseType, Map urlVariables)
        Map<String,String> params = new HashMap<>();
        params.put("name","mm");
        ResponseEntity<String> responseEntity2 = restTemplate.getForEntity(CALL_SERVICE + "/get_String2?name={name}", String.class, params);
        logger.info("getForEntity2:" + responseEntity2.getBody());
        //getForEntity(URI uri, Class responseType)
        UriComponents uriComponents = UriComponentsBuilder.fromUriString(CALL_SERVICE + "/get_String2?name={name}")
                .build().expand("mm").encode();   //这里的expand接收上面两种方式的参数Object… urlVariables和Map
        URI uri = uriComponents.toUri();
        ResponseEntity<String> responseEntity3 = restTemplate.getForEntity(uri, String.class);
        logger.info("getForEntity3:" + responseEntity3.getBody());
        //getForObject(String url, Class responseType, Object… urlVariables)
        String result1 = restTemplate.getForObject(CALL_SERVICE + "/get_String2?name={1}", String.class, "mm");
        logger.info("getForObject1:" + result1);
        //getForObject(String url, Class responseType, Map urlVariables)
        String result2 = restTemplate.getForObject(CALL_SERVICE + "/get_String2?name={name}", String.class, params);
        logger.info("getForObject2:" + result2);
        //getForObject(URI url, Class responseType)
        String result3 = restTemplate.getForObject(uri, String.class);
        logger.info("getForObject3:" + result3);

    }
}
